using System.Collections.Generic;

using Microsoft.SharePoint;

using RealDolmen.Helpers;

namespace RealDolmen.SharePoint.Helpers
{
    public class SPTextParser : TextParser
    {
        private SPListItem ListItem;
        private SPList List;

        public static SPTextParser GetParser(SPListItem listItem)
        {
            return new SPTextParser(listItem);
        }

        public static SPTextParser GetParser(SPList list)
        {
            return new SPTextParser(list);
        }

        protected SPTextParser(SPListItem listItem)
        {
            ListItem = listItem;
            List = ListItem.ParentList;
        }

        protected SPTextParser(SPList list)
        {
            List = list;
        }

        protected override string GetString(IStringDictionary dictionary, string key, string whole, List<string> attributes, string prefix)
        {
            string value = null;

            if (prefix == "item")
            {
                if (ListItem != null)
                {
                    value = WrapperObject.GetPropertyAsString(ListItem, key);

                    // GetFormattedValue throws an exception
                    // return CurrentListItem.GetFormattedValue(key);
                    if (value == null)
                        value = SPListItemHelper.GetString(ListItem, key);
                }
            }
            else if (prefix == "list")
            {
                if (List != null)
                {
                    value = WrapperObject.GetPropertyAsString(List, key);
                }
            }
            else if (prefix == "folder")
            {
                SPFolder folder = null;

                if (ListItem != null)
                {
                    string url = ListItem.Url;
                    int idx = url.LastIndexOf('/');
                    url = url.Remove(idx);


                    SPWeb web = List.ParentWeb;
                    folder = web.GetFolder(url);
                    // SPHelper.TryDisposeWeb(web);
                }
                else if (List != null)
                {
                    folder = List.RootFolder;
                }


                if (folder != null)
                {
                    value = WrapperObject.GetPropertyAsString(folder, key);

                    // GetFormattedValue throws an exception
                    // return CurrentListItem.GetFormattedValue(key);
                    if (value == null)
                        value = SPListItemHelper.GetString(folder.Item, key);
                }
            }
            else if (prefix == "web")
            {
                if (List != null)
                {
                    SPWeb web = List.ParentWeb;

                    value = WrapperObject.GetPropertyAsString(web, key);

                    if (value == null)
                    {
                        object objvalue = web.AllProperties[key];
                        if (objvalue != null)
                            return objvalue.ToString();
                    }
                    // SPHelper.TryDisposeWeb(web);
                }
            }
            else if (prefix == "site")
            {
                if (List != null)
                {
                    SPWeb web = List.ParentWeb;
                    SPSite site = web.Site;

                    value = WrapperObject.GetPropertyAsString(site, key);
                    
                    // SPHelper.TryDisposeSite(site);
                    // SPHelper.TryDisposeWeb(web);
                }
            }

            if (value == null)
                return base.GetString(dictionary, key, whole, attributes, prefix);
            else
                return value;
        }

        protected override string GetString(IStringDictionary dictionary, string key, string whole, List<string> attributes)
        {
            string value = GetString(dictionary, key, whole, attributes, "item");

            if (value == null)
                return base.GetString(dictionary, key, whole, attributes);
            else
                return value;
        }
    }
}
